package com.bgds.sys.common.exception;


import com.bgds.sys.common.pagevo.Result;
import com.bgds.sys.common.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author lj
 * 全局异常处理
 */
@Slf4j
@RestControllerAdvice
public class GlobalException {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result<Object> sysExceptionHandler(HttpServletRequest request, Exception ex) {
        log.error("异常："+ex.getMessage());
        return new ResultUtil<Object>().setErrorMsg(500,ex.getMessage());
    }


    @ExceptionHandler(value = BusinessException.class)
    public Object returnCustomException(BusinessException c){
        log.error("自定义异常："+c.getMessage());
        return new ResultUtil<Object>().setErrorMsg(c.getMessage());
    }

    @ExceptionHandler(value = NullPointerException.class)
    public Object returnNullPointerException(NullPointerException c){
        log.error("空指针异常："+c.getMessage());
        return new ResultUtil<Object>().setErrorMsg(c.getMessage());
    }

    @ExceptionHandler(value = IOException.class)
    public Object returnIoException(IOException c){
        log.error("IO异常："+c.getMessage());
        return new ResultUtil<Object>().setErrorMsg(c.getMessage());
    }
}
